package net.homeftp.heimo.weddingwidget.core;

import java.util.Calendar;
import java.util.Date;

public class DateHelper {
	private static final long ONEDAY = 86400000L;
	public static long getAlarmWakeUpTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTimeInMillis();
	}
	
	public static int getCorrectedDay(Date checkDate, Date targetDate){
		long diff = targetDate.getTime() - checkDate.getTime();
		long days = Math.abs((diff/(ONEDAY)));
		return (int) Math.floor(Math.abs(days));
	}
	
	public static CorrectDate getCorrectedDate(Date checkDate, Date targetDate){
		CorrectDate cDate = new CorrectDate();
        if(checkDate!= null && targetDate!=null){
        	int dayInMonth, dayInMonthCheck,dayInLastMonthTarget = 31;
        	
        	if(checkDate.after(targetDate)){
        		Date tempDate = null;
        		tempDate = (Date)checkDate.clone();
        		checkDate = (Date)targetDate.clone();		
        		targetDate = tempDate;
        	}
        	
        	Calendar checkDt = Calendar.getInstance();
            checkDt.setTime(checkDate);  
            int year = checkDt.get(Calendar.YEAR);
            int month = checkDt.get(Calendar.MONTH);
            int day = checkDt.get(Calendar.DATE);
            dayInMonthCheck = checkDt.getActualMaximum(Calendar.DATE);

            Calendar targetDT = Calendar.getInstance();
            targetDT.setTime(targetDate);
            int yearT = targetDT.get(Calendar.YEAR);
            int monthT = targetDT.get(Calendar.MONTH);
            int dayT = targetDT.get(Calendar.DATE);
            
            if(yearT==year && monthT==month && dayT==day){
            	cDate.setDay(0);
            	return cDate;
            }
            
            Calendar targetDtLastMonth = (Calendar) targetDT.clone();
			targetDtLastMonth.add(Calendar.MONTH, -1);
			dayInLastMonthTarget = targetDtLastMonth.getActualMaximum(Calendar.DATE);
			
			dayInMonth = (dayInMonthCheck>dayInLastMonthTarget?dayInMonthCheck:dayInLastMonthTarget);
            
            
            if (dayT < day) {
                day = dayInMonth - day + dayT;
                if ((monthT - 1) - month < 0) {
                    month = (monthT - 1) - (month - 12);
                    year = yearT - year - 1;
                } else {
                    month = monthT - month - 1;
                    year = yearT - year;
                }
            } else {
                day = dayT - day;
                if (monthT - month < 0) {
                    month = monthT - (month - 12);
                    year = yearT - year - 1;
                } else {
                    month = monthT - month;
                    year = yearT - year;
                }
            }
            cDate.setYear(year);
            cDate.setMonth(month);
            cDate.setDay(day);
        }
        return cDate;
    }
}
